<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<!--{include file='comm/header.tpl'}-->
	<title>发放账户优惠券</title>
</head>

<body>
    <div style="width:100%">
        <div class="container">

            <div class="comm_content">

                <div class="panel panel-default">

                    <div class="panel-heading">正在操作：发放账户优惠券</div>

                    <div class="panel-body">

                        <form id="searchForm" class="form-inline" name="searchForm" onsubmit="return false;">
                            <table class="ebsig-text">
                                <tr>
                                    <td width="80%">会员名：&nbsp;<input type="text" id="custID" class="form-control input-sm" name="custID" value="" style="width:250px;"/></td>
                                    <td>
                                        <input type="button" id="che_btn" value="查询" class="btn btn-primary btn-sm">&nbsp;&nbsp;
                                        <input type="button" class="btn btn-primary btn-sm" value="重置" onclick="coupon.clear();">
                                    </td>
                                </tr>
                            </table>
                        </form>

                        <div style="height: 15px;"></div>

                        <form name="selectorNameFrm" id="selectorNameFrm" method="post">
                            <table class="form-inline">
                                <tr>
                                    <td style="text-align:left;width: 300px;">
                                        <select id="allSource" class="form-control" name="allSource" style="width: 100%;height:270px;" multiple></select>
                                    </td>
                                    <td style="text-align:center;width: 20%;">
                                        <div>
                                            <input type="button" value=">" style="width: 35px;" id="rightMv" name="rightMv" class="btn btn btn-success btn-sm" >
                                        </div>
                                        <div style="margin-top:20px;">
                                            <input type="button" value="<" style="width: 35px;" id="leftMv" name="leftMv" class="btn btn btn-success btn-sm" >
                                        </div>
                                        <div style="margin-top:20px;">
                                            <input type="button" value=">>" style="width: 35px;" id="rightAllMv" name="rightAllMv" class="btn btn btn-success btn-sm" >
                                        </div>
                                        <div style="margin-top:20px;">
                                            <input type="button" value="<<" style="width: 35px;" id="leftAllMv" name="leftAllMv" class="btn btn btn-success btn-sm" >
                                        </div>
                                    </td>
                                    <td style="width: 300px;">
                                        <select id="publishSource" class="form-control" name="publishSource[]" style="width: 100%;height:270px;" multiple></select>
                                    </td>
                                </tr>
                            </table>
                        </form>

                    </div>
                </div>

                <div class="popup-footer" style="padding-top: 15px;">
                    <div class="rectbox tac">
                        <input type='button' value='保存' onclick='coupon.save();' class='btn btn-success'/>&nbsp;&nbsp;&nbsp;
                        <input type="button" class="btn btn-default" onclick="E.closeWindows();" value="关闭" />
                    </div>
                </div>

            </div>

        </div>
    </div>
</body>
</html>
<script type="text/javascript">

    var couponID = <!--{$couponID}-->;

	$(document).ready(function (){
		//向右移
		function rightMv_fun(event){

			var flg = event.data.foo;

			if(flg == 0){
				var mvValue = $("#allSource").val();
			}else{
				var mvValue = $("#allSource option");
			}
			var mvLen = 0;
			if(mvValue){
				mvLen = mvValue.length;
			}

			var option_Str = $("#publishSource").html();
			for(var i = 0;i < mvLen;i++){
				if(flg == 0){
					var val = mvValue[i];
				}else{
					var val = mvValue[i].value;
				}
				var rightText = $("#publishSource option[value='" + val + "']").text();
				if(rightText == ""){
					var mvText = jQuery.trim($("#allSource option[value='" + val + "']").text());
					option_Str += "<option value=" + val + ">" ;
					option_Str += mvText;
					option_Str += "</option>" ;
				}
			}

			$("#publishSource").html(option_Str);
		}
		//移除
		function del(event){

			var flg = event.data.foo;

			if(flg == 0){
				var val = $("#publishSource").val();
			}else{
				var val = $("#publishSource option");
			}

			if(val){
				var val_len = val.length;
			}

			for(var i = 0;i < val_len;i++){
				if(flg == 0){
					$("#publishSource option[value='" + val[i] + "']").remove();
				}else{
					var mv_val = val[i].value;
					$("#publishSource option[value='" + mv_val + "']").remove();
				}
			}

		}

        //查询用户名
        $("#che_btn").click(function () {
            var custID = $.trim($("#custID").val());

            if(custID == ""){
                E.alert('请输入要查找的用户名');
            } else {
                E.ajax_get({
                    url: "/invokingAjax-backend/customer/customer.ajax?operFlg=4",
                    data: {
                        custID: custID
                    },
                    call: "coupon.getUserCallBack"
                });
            }
        });

		$("#rightMv").bind('click',{foo:0},rightMv_fun);
		$("#leftMv").bind('click',{foo:0},del);
		$("#rightAllMv").bind('click',{foo:1},rightMv_fun);
		$("#leftAllMv").bind('click',{foo:1},del);

	})

	var coupon  = {

        /**
         * 查询用户名的回调
         */
        getUserCallBack : function ( o ) {

            if (o.code == 200) {
                var html = "<option value='"+ o.data.pcustID+"'>";
                html += o.data.custID;
                html += "</option>";
                $("#allSource").html(html);
            } else {
                E.alert(o.message);
            }

        },

        //保存
        save : function (){
            var custID = { };
            $("#publishSource option").each(function (k, v){
                custID[$(v).val()] = $.trim($(v).text());
            });

            if($.isEmptyObject(custID)){
                E.alert('发放的用户名不能为空');
                return false;
            }
            coupon.ajax(7,{data : custID,couponID : couponID},coupon.saveCallBack);
        },

        /**
         * 重置表单
         */
        clear : function() {
            document.forms['searchForm'].reset();
        },

        /**
         *保存回调
         */
        saveCallBack : function (respose){
            if(respose.code != 200){
                E.alert(respose.message);
                return false;
            }else{
                E.alert(respose.message,2);
            }
            //E.alert('发放成功',2,"E.refresh()");
        },

        /**
         * ajax
         * @param flg  operFlg
         * @param data  提交数据
         * @param func  回调方法
         */
        ajax : function (flg,data,func){
            $.ajax({
                type: "POST",
                url: "/invokingAjax-backend/promotion/coupon.ajax?operFlg="+flg,
                dataType: "JSON",
                data: data,
                success: function( response ) {
                    func(response);
                }
            });
        }
    };


</script>